package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.Assert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.ATeamInfo;
import com.ruoyi.system.domain.AUser;
import com.ruoyi.system.domain.vo.ATeamInfoVO;
import com.ruoyi.system.domain.vo.ATeamVO;
import com.ruoyi.system.domain.vo.XcxATeamInfoVO;
import com.ruoyi.system.domain.vo.XcxOperTeamInfoVO;
import com.ruoyi.system.mapper.ATeamInfoMapper;
import com.ruoyi.system.mapper.ATeamMapper;
import com.ruoyi.system.mapper.AUserMapper;
import com.ruoyi.system.service.IATeamInfoService;
import com.ruoyi.system.util.LoginUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 团队成员Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-16
 */
@Service
@Transactional
public class ATeamInfoServiceImpl implements IATeamInfoService
{
    @Autowired
    private ATeamInfoMapper aTeamInfoMapper;

    @Autowired
    private ATeamMapper aTeamMapper;

    @Autowired
    private AUserMapper aUserMapper;


    /**
     * 查询团队成员
     *
     * @param id 团队成员主键
     * @return 团队成员
     */
    @Override
    public ATeamInfo selectATeamInfoById(Long id)
    {
        return aTeamInfoMapper.selectATeamInfoById(id);
    }

    /**
     * 查询团队成员列表
     *
     * @param aTeamInfo 团队成员
     * @return 团队成员
     */
    @Override
    public List<ATeamInfo> selectATeamInfoList(ATeamInfo aTeamInfo)
    {
        return aTeamInfoMapper.selectATeamInfoList(aTeamInfo);
    }

    /**
     * 新增团队成员
     *
     * @param aTeamInfo 团队成员
     * @return 结果
     */
    @Override
    public int insertATeamInfo(ATeamInfo aTeamInfo)
    {
        aTeamInfo.setCreateTime(DateUtils.getNowDate());
        return aTeamInfoMapper.insertATeamInfo(aTeamInfo);
    }

    /**
     * 修改团队成员
     *
     * @param aTeamInfo 团队成员
     * @return 结果
     */
    @Override
    public int updateATeamInfo(ATeamInfo aTeamInfo)
    {
        aTeamInfo.setUpdateTime(DateUtils.getNowDate());
        return aTeamInfoMapper.updateATeamInfo(aTeamInfo);
    }

    /**
     * 批量删除团队成员
     *
     * @param ids 需要删除的团队成员主键
     * @return 结果
     */
    @Override
    public int deleteATeamInfoByIds(Long[] ids)
    {
        //移除团员前，先变更用户的状态
        for (int i = 0 ; i<ids.length; i++){
            ATeamInfo info =  aTeamInfoMapper.selectATeamInfoById(ids[i]);
            AUser us = aUserMapper.selectAUserById(info.getUserId());
            us.setRole("2");//变更为普通用户
            aUserMapper.updateAUser(us);
        }
        return aTeamInfoMapper.deleteATeamInfoByIds(ids);
    }

    /**
     * 删除团队成员信息
     *
     * @param id 团队成员主键
     * @return 结果
     */
    @Override
    public int deleteATeamInfoById(Long id)
    {
        return aTeamInfoMapper.deleteATeamInfoById(id);
    }

    @Override
    public int addTeamInfors(Long[] ids) {
        long teamId = ids[ids.length-1];
        for (int i = 0; i <= (ids.length - 2); i++){
            System.out.println(ids[i]);
            ATeamInfo info = new ATeamInfo();
            info.setTeamId(teamId);
            info.setUserId(ids[i]);
            info.setIsAgent("1");//是否是代理商0是1否
            info.setApplicationTime(DateUtils.getNowDate());
            info.setCreateTime(DateUtils.getNowDate());
            aTeamInfoMapper.insertATeamInfo(info);

            AUser user = aUserMapper.selectAUserById(ids[i]);
            user.setRole("1");//0代理商1团对成员2普通用户
            aUserMapper.updateAUser(user);
        }
        return 1;
    }

    @Override
    public List<ATeamInfoVO> selectATeamInfoLists(ATeamInfoVO aTeamInfovo) {
        return aTeamInfoMapper.selectATeamInfoLists(aTeamInfovo);
    }

    @Transactional
    @Override
    public int handleupteAgent(ATeamInfo aTeamInfo) {
        aTeamInfoMapper.handleupteAgent(aTeamInfo.getTeamId());
        aTeamInfo.setIsAgent("0");
        return aTeamInfoMapper.updateATeamInfo(aTeamInfo);
    }

    @Override
    public List<XcxATeamInfoVO> selectATeamInfoXcxList(ATeamInfo aTeamInfo) {
        aTeamInfo.setUserId(LoginUtils.getUser().getId());
        return aTeamInfoMapper.selectATeamInfoXcxList(aTeamInfo);
    }


    //-----------------------------------------
    @Override
    public int teamOperation(XcxOperTeamInfoVO vo) {
        int i = 0;
        //自己不能加入自己
        Assert.notEquals(LoginUtils.getUser().getId(),vo.getUserId(),"用户能不邀请自己");

        //在申请中的请勿重复提交

        //0申请入团 1同意 2驳回 3退出团队操作
        if("0".equals(vo.getState())){
            ATeamInfo info = new ATeamInfo();
            info.setReviewStatus("0");
            info.setApplicationTime(DateUtils.getNowDate());
            info.setTeamId(vo.getTeamId());
            info.setUserId(vo.getUserId());
            i = aTeamInfoMapper.insertATeamInfo(info);
        }
        if("1".equals(vo.getState())){
            ATeamInfo info = new ATeamInfo();
            info.setId(vo.getInforId());
            info.setReviewStatus("1");
            info.setThroughTime(DateUtils.getNowDate());
            aTeamInfoMapper.updateATeamInfo(info);
            //修改用户的权限
            AUser us = new AUser();
            us.setId(vo.getUserId());
            us.setRole("1");
            i = aUserMapper.updateAUser(us);
            //团队成员数量+1
            ATeamInfo at = aTeamInfoMapper.selectATeamInfoById(vo.getInforId());
            aTeamMapper.addTeamNum(at.getTeamId());
        }
        if("2".equals(vo.getState())){
            ATeamInfo info = new ATeamInfo();
            info.setId(vo.getInforId());
            i = aTeamInfoMapper.deleteATeamInfoById(vo.getInforId());
        }
        if("3".equals(vo.getState())){
            //修改用户的权限
            AUser us = new AUser();
            us.setId(vo.getUserId());
            us.setRole("2");
            aUserMapper.updateAUser(us);

            ATeamInfo info = new ATeamInfo();
            info.setId(vo.getInforId());
            i = aTeamInfoMapper.deleteATeamInfoById(vo.getInforId());

            //团队成员数量-1
            ATeamInfo at = aTeamInfoMapper.selectATeamInfoById(vo.getInforId());
            aTeamMapper.reduceTeamNum(at.getTeamId());
        }
        return i;
    }

    @Override
    public int teamJoin(XcxOperTeamInfoVO vo) {
        Long userId = LoginUtils.getUser().getId();
        //自己不能加入自己
        ATeamInfo infor = new ATeamInfo();
        infor.setUserId(userId);
        ATeamInfo atInfo = aTeamInfoMapper.selectATeamInfoByUser(infor);
        Assert.Null(atInfo,"您已加入团队！");
        ATeamVO atem =  aTeamMapper.selectATeamById(vo.getTeamId());
        Assert.notEquals(atem.getUserId(),LoginUtils.getUser().getId(),"用户已经是团长！");

        ATeamInfo info = new ATeamInfo();
        info.setReviewStatus("1");
        info.setApplicationTime(DateUtils.getNowDate());
        info.setCreateTime(DateUtils.getNowDate());
        info.setTeamId(vo.getTeamId());
        info.setUserId(vo.getUserId());
        info.setIsAgent("1");

        //修改用户角色
        AUser aUser = new AUser();
        aUser.setId(userId);
        aUser.setRole("1");
        aUserMapper.updateAUser(aUser);
        return aTeamInfoMapper.insertATeamInfo(info);
    }

    @Override
    public int agreeTeam(XcxOperTeamInfoVO vo) {
        ATeamInfo infoById = selectATeamInfoById(vo.getInforId());
        Assert.notNull(infoById,"数据不能为空");
        Assert.notNull(infoById.getUserId(),"用户不能为空");
        AUser user = aUserMapper.selectAUserById(infoById.getUserId());
        Assert.notEmpty(user,"用户不存在");
        Assert.notEquals(user.getRole(),1L,"当前用户已经加如团队了!");
        Assert.notEquals(user.getRole(),0L,"用户已经是团长");

        ATeamInfo info = new ATeamInfo();
        info.setId(infoById.getId());
        info.setReviewStatus("1");
        info.setThroughTime(DateUtils.getNowDate());
        aTeamInfoMapper.updateATeamInfo(info);

        //修改用户的权限
        AUser us = new AUser();
        us.setId(infoById.getUserId());
        us.setRole("1");
        aUserMapper.updateAUser(us);

        //团队成员数量+1
        ATeamInfo at = aTeamInfoMapper.selectATeamInfoById(vo.getInforId());
        return aTeamMapper.addTeamNum(at.getTeamId());
    }

    @Override
    public int rejectTeam(XcxOperTeamInfoVO vo) {
        ATeamInfo info = new ATeamInfo();
        info.setId(vo.getInforId());
        return aTeamInfoMapper.deleteATeamInfoById(vo.getInforId());
    }

    @Override
    public int quitTeam() {
        ATeamInfo info = new ATeamInfo();
        info.setUserId(LoginUtils.getUser().getId());
        ATeamInfo aTeamInfo = aTeamInfoMapper.selectATeamInfoByUser(info);

        //修改用户的权限
        AUser us = new AUser();
        us.setId(LoginUtils.getUser().getId());
        us.setRole("2");
        aUserMapper.updateAUser(us);

        //团队成员数量-1
        aTeamMapper.reduceTeamNum(aTeamInfo.getTeamId());

        return aTeamInfoMapper.deleteATeamInfoById(aTeamInfo.getId());
    }

    @Override
    public int delTeamInfo(ATeamInfo aTeamInfo) {
        Long currentId = LoginUtils.getUser().getId();
        Long teamId = aTeamInfo.getTeamId();
        Long userId = aTeamInfo.getUserId();
        Assert.notEquals(currentId,userId,"团队创建者无法被移除");
        ATeamVO aTeamVO = aTeamMapper.selectATeamById(teamId);
        Assert.notNull(aTeamVO,"团队不存在");
        Long agentUserId = aTeamVO.getUserId();
        Assert.equals(currentId, agentUserId,"非团队创建者，无法删除");

        ATeamInfo info = new ATeamInfo();
        info.setUserId(userId);
        info.setTeamId(teamId);
        ATeamInfo teamInfo = aTeamInfoMapper.selectATeamInfoByUser(info);

        //修改用户的权限
        AUser us = new AUser();
        us.setId(userId);
        us.setRole("2");
        aUserMapper.updateAUser(us);
        //团队成员数量-1
        aTeamMapper.reduceTeamNum(teamId);
        return aTeamInfoMapper.deleteATeamInfoById(teamInfo.getId());
    }
}
